package cn.com.header.zbpk.account.controller;

import cn.com.header.core.IPage;
import cn.com.header.core.ISort;
import cn.com.header.core.page.SimplePage;
import cn.com.header.core.sort.Sorter;
import cn.com.header.core.support.web.JsonViewData;
import cn.com.header.core.support.web.ResultCode;
import cn.com.header.zbpk.account.entity.Role;
import cn.com.header.zbpk.account.service.IRoleService;
import cn.com.header.zbpk.web.CommonController;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 角色前端控制器
 *
 * @author luohaidian
 * @date 2017-11-18
 * @time 10:14
 */
@RestController
@RequestMapping(value = "/manage/account/role", method = RequestMethod.POST)
public class RoleController extends CommonController {

    @Autowired
    private IRoleService iRoleService;

    /**
     * 角色列表
     * @param pageNo
     * @return
     */
    @RequestMapping(value = "/list")
    public JsonViewData list(
            @RequestParam(name = "pageNo", defaultValue = "1") int pageNo){
        IPage page = new SimplePage(pageNo, pageSize);
        Set<String> fields = Sets.newHashSet("id", "name", "create_time AS createTime", "remark");
        Set<ISort> sortSet = Sets.newHashSet(new Sorter("create_time", true));
        Role role = new Role();
        role.setSchoolId(getCurrentUser().getSchoolId());
        int totalRecordCount = iRoleService.loadCount(role);
        List<? extends Role> roleExtendList = totalRecordCount == 0 ? Collections.EMPTY_LIST : iRoleService.customLoads(role, fields, sortSet, page);
        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("roleList", roleExtendList);
        resultMap.put("totalRecordCount", totalRecordCount);
        return setJsonViewData(resultMap);
    }

    /**
     * 新增角色
     * @param role
     * @return
     */
    @RequestMapping(
            value = "/push/create",
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public JsonViewData create(@RequestBody Role role){
        Role saveRole = new Role();
        saveRole.setName(role.getName());
        saveRole.setRemark(role.getRemark());
        saveRole.setSchoolId(getCurrentUser().getSchoolId());
        boolean success = iRoleService.saveOrUpdate(saveRole);
        return success ? setJsonViewData(saveRole.getId()) : setJsonViewData(ResultCode.FAIL);
    }

    /**
     * 编辑角色
     * @param role
     * @return
     */
    @RequestMapping(
            value = "/push/edit",
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public JsonViewData update(@RequestBody Role role){
        Role tempRole = new Role();
        tempRole.setId(role.getId());
        tempRole.setName(role.getName());
        tempRole.setRemark(Objects.isNull(role.getRemark()) ? "" : role.getRemark());
        boolean success = iRoleService.saveOrUpdate(tempRole);
        return success ? setJsonViewData(ResultCode.SUCCESS) : setJsonViewData(ResultCode.FAIL);
    }

    /**
     * 角色详情
     * @param roleId
     * @return
     */
    @RequestMapping(value = "preview/{roleId}")
    public JsonViewData preview(
            @PathVariable(name = "roleId") Long roleId){
        Optional<Role> optional = iRoleService.loadByPK(roleId, null);
        if (!optional.isPresent()){
            return setJsonViewData(ResultCode.PARAM_ERROR, "角色ID[" + roleId + "]有误！！！");
        }
        Map<String, Object> dataMap = iRoleService.queryMapByRoleId(roleId);
        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("role", dataMap);
        return setJsonViewData(resultMap);
    }

    /**
     * 角色选项卡
     * @return
     */
    @RequestMapping(value = "/options")
    public JsonViewData options(){
        List<Role> roleList = iRoleService.queryBy(getCurrentUser().getSchoolId());
        Map<String, Object> resultMap = Maps.newHashMapWithExpectedSize(1);
        resultMap.put("roleList", roleList);
        return setJsonViewData(resultMap);
    }

    /**
     * 角色成员列表
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/user/list")
    public JsonViewData getRoleUserList(
            @RequestParam(name = "roleId") Long roleId){
        Optional<Role> optional = iRoleService.loadByPK(roleId, Sets.newHashSet("id", "name"));
        if (!optional.isPresent()){
            return setJsonViewData(ResultCode.PARAM_ERROR, "角色ID[" + roleId + "]有误！！！");
        }
        Map<String,Object> resultMap = iRoleService.queryRoleUser(roleId);
        return setJsonViewData(resultMap);
    }

    /**
     * 角色删除
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/push/delete/{id}")
    public JsonViewData delete(
            @PathVariable(name = "id") Long roleId){
        Optional<Role> optional = iRoleService.loadByPK(roleId, null);
        if (!optional.isPresent()){
            return setJsonViewData(ResultCode.PARAM_ERROR, "删除的角色ID[" + roleId + "]有误！！！");
        }
        boolean success = iRoleService.delete(roleId);
        return success ? setJsonViewData(ResultCode.SUCCESS) : setJsonViewData(ResultCode.FAIL);
    }

}
